namespace Portal.Modules.AdminPortal
{
	using System;
	using System.Collections;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	/// <summary>
	///		Summary description for Tab.
	/// </summary>
	public partial class TabControl : Tab
	{

		private PortalDefinition.Tab currentTab = null;

		public delegate void TabEventHandler(object sender, PortalDefinition.Tab tab);
		private string portalId = "";
		public event TabEventHandler Save = null;
		public event TabEventHandler Cancel = null;
		public event TabEventHandler Delete = null;

		protected void Page_Load(object sender, System.EventArgs e)
		{
			if (Request.Params["tabRef"] != null)
			{
				portalId = Helper.GetPortalConfig(Request.Params["tabRef"].ToLower());
			}
		}

		protected override void LoadViewState(object bag)
		{
			base.LoadViewState(bag);
			currentTab = Helper.GetEditTab((string)ViewState["CurrentTab"]);
		}
	
		protected override object SaveViewState()
		{
			string tabReference = "";
			if (currentTab != null)
				tabReference = currentTab.reference;
			ViewState["CurrentTab"] = tabReference;
			return base.SaveViewState();
		}

		private void ShowEditModules()
		{
			editModule.Visible = true;
			listModule_Left.Visible = false;
			listModule_Middle.Visible = false;
			listModule_Right.Visible = false;
		}
	
		private void ShowModulesList()
		{
			editModule.Visible = false;
			listModule_Left.Visible = true;
			listModule_Middle.Visible = true;
			listModule_Right.Visible = true;
		}

		override public void LoadData(PortalDefinition.Tab t)
		{
			currentTab = t;

			txtTitle.Text = HttpUtility.HtmlDecode(t.title);
			txtReference.Text = currentTab.reference;
			txtImagePathI.Text = HttpUtility.HtmlDecode(t.imgPathInactive);
			txtImagePathA.Text = HttpUtility.HtmlDecode(t.imgPathActive);

			listRoles.LoadData(t.roles);
			listModule_Left.LoadData(t.left);
			listModule_Middle.LoadData(t.middle);
			listModule_Right.LoadData(t.right);
		}

		override public void EditModule(string reference)
		{
			ShowEditModules();
			editModule.LoadData(currentTab.reference, reference);
		}

		override public void AddModule(ModuleList list)
		{
			PortalDefinition pd = PortalDefinition.Load();
			PortalDefinition.Tab t = currentTab;

			PortalDefinition.Module m = PortalDefinition.Module.Create();

			if (list == listModule_Left)
			{
				t.left.Add(m);
			}
			else if (list == listModule_Middle)
			{
				t.middle.Add(m);
			}
			else if (list == listModule_Right)
			{
				t.right.Add(m);
			}

			pd.Save();

			// Rebind
			LoadData(currentTab);

			EditModule(m.reference);
		}

		override public void AddModule(ModuleList list, string portalID)
		{
			PortalDefinition pd = PortalDefinition.Load(portalID);
			PortalDefinition.Tab t = currentTab;

			PortalDefinition.Module m = PortalDefinition.Module.Create();

			if (list == listModule_Left)
			{
				t.left.Add(m);
			}
			else if (list == listModule_Middle)
			{
				t.middle.Add(m);
			}
			else if (list == listModule_Right)
			{
				t.right.Add(m);
			}

			pd.Save(portalID);

			LoadData(currentTab);

			EditModule(m.reference);
		}

		protected void OnCancelEditModule(object sender, EventArgs args)
		{
			ShowModulesList();
		}

		protected void OnSaveModule(object sender, EventArgs args)
		{
			// Rebind
			LoadData(currentTab);
			ShowModulesList();
		}

		protected void OnDeleteModule(object sender, EventArgs args)
		{
			// Rebind
			LoadData(currentTab);
			ShowModulesList();
		}

		override public void MoveModuleUp(int idx, ModuleList list)
		{
			if (idx <= 0) return;

			PortalDefinition pd = PortalDefinition.Load();
			PortalDefinition.Tab t = currentTab;

			ArrayList a = null;

			if (list == listModule_Left)
			{
				a = t.left;
			}
			else if (list == listModule_Middle)
			{
				a = t.middle;
			}
			else if (list == listModule_Right)
			{
				a = t.right;
			}

			PortalDefinition.Module m = (PortalDefinition.Module)a[idx];
			a.RemoveAt(idx);
			a.Insert(idx - 1, m);

			pd.Save();

			// Rebind
			LoadData(currentTab);
		}

		override public void MoveModuleDown(int idx, ModuleList list)
		{
			PortalDefinition pd = PortalDefinition.Load();
			PortalDefinition.Tab t = currentTab;

			ArrayList a = null;

			if (list == listModule_Left)
			{
				a = t.left;
			}
			else if (list == listModule_Middle)
			{
				a = t.middle;
			}
			else if (list == listModule_Right)
			{
				a = t.right;
			}

			if (idx >= a.Count - 1) return;

			PortalDefinition.Module m = (PortalDefinition.Module)a[idx];
			a.RemoveAt(idx);
			a.Insert(idx + 1, m);

			pd.Save();

			// Rebind
			LoadData(currentTab);
		}

		protected void OnCancel(object sender, EventArgs args)
		{
			PortalDefinition pd = PortalDefinition.Load(Helper.GetPortalConfig(currentTab.reference));
			PortalDefinition.Tab t = currentTab;

			if (Cancel != null)
			{
				Cancel(this, t);
			}

			LoadData(currentTab);
			ShowModulesList();
		}

		protected void OnSave(object sender, EventArgs args)
		{
			try
			{
				if (!Page.IsValid) return;

				PortalDefinition pd = PortalDefinition.Load(Helper.GetPortalConfig(currentTab.reference));
				PortalDefinition.Tab t = currentTab;

				t.title = HttpUtility.HtmlEncode(txtTitle.Text);
				t.reference = txtReference.Text;
				t.imgPathInactive = HttpUtility.HtmlEncode(txtImagePathI.Text);
				t.imgPathActive = HttpUtility.HtmlEncode(txtImagePathA.Text);
				t.roles = listRoles.GetData();

				pd.Save(Helper.GetPortalConfig(currentTab.reference));

				currentTab = t;

				if (Save != null)
				{
					Save(this, t);
				}

				ShowModulesList();
			}
			catch (Exception e)
			{
				lbError.Text = e.Message;
			}
		}

		protected void OnDelete(object sender, EventArgs args)
		{
			PortalDefinition pd = PortalDefinition.Load(Helper.GetPortalConfig(currentTab.reference));
			PortalDefinition.Tab t = currentTab;

			PortalDefinition.DeleteTab(currentTab.reference, Helper.GetPortalConfig(currentTab.reference));

			if (Delete != null)
			{
				Delete(this, t);
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}

		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{

		}
		#endregion
	}
}
